package org.example.exception;

import org.apache.commons.io.FileUtils;
import org.example.utlis.FXUtils;
import org.example.utlis.TimeUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Time;

public class ExceptionReporter {
    private static final String PATH = "../../app-error";
    public static void saveLog(Exception e, boolean isRemind) {
        if (isRemind) {
            FXUtils.initSimpleDialog("出错！", e.toString());
        }
        new Thread(() -> {
            String currentTime = TimeUtils.getCurrentTimeForFileName();
            String fileName = PATH + "/" + currentTime + ".txt";
            try {
                StackTraceElement[] stackTrace = e.getStackTrace();
                File file = new File(fileName);
                StringBuffer sb = new StringBuffer();
                sb.append(e + "\n");
                for (StackTraceElement line : stackTrace) {
                    sb = sb.append(line + "\n");
                }
                FileUtils.writeStringToFile(file, sb.toString());
            } catch (IOException ex) {
                FXUtils.initSimpleDialog("日志写入出错！", ex.toString());
            }
        }).start();
    }
}
